use actix_web::{web, App, HttpServer};
use best_data_server::utils::config;
use best_data_server::{controller, db, AppState};
use std::sync::{Arc, Mutex};

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    // get config
    let conf = config::Config::new("config.json");

    // init db

    // init app data
    let app_state = web::Data::new(AppState {
        connections: Mutex::new(0),
        seq: Arc::new(db::Seq::new(&conf.get_database_url().as_str(), &conf.get_mongo_uri()).await),
    });

    // create server
    HttpServer::new(move || {
        App::new()
            .app_data(app_state.clone())
            .configure(controller::init)
    })
    .bind(conf.get_uri())?
    .run()
    .await
}
